package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.XunjianWoTjController;
import com.antoco.operation.bigscreen.model.dto.XunjianWoTjDTO;
import com.antoco.operation.bigscreen.model.entity.XunjianWoTj;
import com.antoco.operation.bigscreen.second.ValidList;
import com.antoco.operation.bigscreen.mapper.XunjianWoTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.XunjianWoTjService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/xunjian-wo-tj")
@RestController
public class XunjianWoTjControllerImpl implements XunjianWoTjController {

    @Autowired
    private XunjianWoTjService xunjianWoTjService;

    @Autowired
    private XunjianWoTjMapper xunjianWoTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public XunjianWoTjDTO save(@RequestBody XunjianWoTjDTO xunjianWoTjDTO) {
        XunjianWoTj xunjianWoTj = xunjianWoTjMapper.asEntity(xunjianWoTjDTO);
        xunjianWoTjService.save(xunjianWoTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return xunjianWoTjDTO;
    }

    @Override
    @PostMapping("save-lists")
    @ResponseStatus(HttpStatus.CREATED)
    public List<XunjianWoTjDTO> saveLists(@Validated @RequestBody ValidList<XunjianWoTjDTO> xunjianWoTjDTOLists) {
        xunjianWoTjService.save(xunjianWoTjMapper.asEntityList(xunjianWoTjDTOLists));
        //当完成数据的操作后，调用该封装的方法获取变化后的数据进行推送
        channelDataService.pushAllLists(this.getClass().getName());
        return xunjianWoTjDTOLists;
    }

    @Override
    @GetMapping("/{id}")
    public XunjianWoTjDTO findById(@PathVariable("id") String id) {
        XunjianWoTj xunjianWoTj = xunjianWoTjService.findById(id).orElse(null);
        return xunjianWoTjMapper.asDTO(xunjianWoTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        xunjianWoTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<XunjianWoTjDTO> list() {
        return xunjianWoTjMapper.asDTOList(xunjianWoTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<XunjianWoTjDTO> pageQuery(Pageable pageable) {
        Page<XunjianWoTj> xunjianWoTjPage = xunjianWoTjService.findAll(pageable);
        List<XunjianWoTjDTO> dtoList = xunjianWoTjPage
                .stream()
                .map(xunjianWoTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, xunjianWoTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public XunjianWoTjDTO update(@RequestBody XunjianWoTjDTO xunjianWoTjDTO, @PathVariable("id") String id) {
        XunjianWoTj xunjianWoTj = xunjianWoTjMapper.asEntity(xunjianWoTjDTO);
        xunjianWoTjMapper.asDTO(xunjianWoTjService.update(xunjianWoTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return xunjianWoTjDTO;
    }
}